
require('jquery_ujs');
require('jquery.dropdown');
import AdminOrderDetail from './components/adminOrderDetail.vue';
import Calendar from './components/calendar.vue'
import Dateformat from 'dateformat'
import ProductPicker from './components/productPicker.vue';
import Vue from 'vue';

import Query from './query'

document.addEventListener('DOMContentLoaded', function () {
  new Vue({
    el: '#app',
    data () {
      return {
        properties: [],
        property: {
          itemType: "product[properties][][item_type]",
          itemTypePlaceholder: "请填写属性名",
          itemValue: "product[properties][][item_value]",
          itemValuePlaceholder: "请填写属性值",
        },
        disabled: [],
        value: Dateformat(new Date(), "yyyy-mm-dd"),
        format: 'yyyy-MM-dd',
        clear: true,
        statedAtName: "promotion[started_at]",
        endedAtName: "promotion[ended_at]",
        salesStatedAtName: "sales_promotion[started_at]",
        salesEndedAtName: "sales_promotion[ended_at]",
        designerPromotionTime: "designer_promotion[publish_time]",
        placeholder: 'placeholder is displayed when value is null or empty'
      }
    },
    components: {
      'adminOrderDetail': AdminOrderDetail,
      'calendar': Calendar,
      'productPicker': ProductPicker
    },
    methods: {
      addProperty(e) {
        e.preventDefault()
        this.properties.push(Vue.util.extend({}, this.property))
      },
      delProperty(index) {
        this.properties.splice(index, 1)
      },
      delExsitedProperty(e) {
        $(e.currentTarget).parents("div.property-unit:first").remove()
      },
      promotionsCallback() {
        return function(id) {
          let _this = this

          Query.get('/admin/promotions/' + id).then((data) => {
            let ids = data.promotion.product_ids.map((item) => {
              return parseInt(item)
            })
            _this.selectedIds = ids
          }).catch((error) => {
            console.log(error.message)
          })

        }
      },
      floorsCallback() {
        return function(id) {
          let _this = this

          Query.get('/admin/floors/' + id).then((data) => {
            let ids = data.floor.product_ids.map((item) => {
              return parseInt(item)
            })
            _this.selectedIds = ids
          }).catch((error) => {
            console.log(error.message)
          })
        }
      },
      productCasesCallback() {
        return function(id) {
          let _this = this

          Query.get('/admin/product_cases/' + id).then((data) => {
            let ids = data.product_case.product_ids.map((item) => {
              return parseInt(item)
            })
            _this.selectedIds = ids
          }).catch((error) => {
            console.log(error.message)
          })
        }
      }
    },
    mounted () {
      let baiduEditor = require('./editor/editor.coffee')('editor-container', {'width': '900px', 'height': '600'})
      if(baiduEditor.umeditor) {
        baiduEditor.doListen(function (umeditor) {
          $('.edui-toolbar button').click(function() {
          })
        })
      }
    }
  })
})
